package com.cop.consultapp.thread;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.SAXException;

import com.cop.consultapp.model.QuestionVO;
import com.cop.consultapp.parser.ConsultParser;

import android.os.Handler;
import android.os.Message;

public class ConsultListThread extends Thread {
	
	private Handler handler = null;
	
	private final static String REQUEST_LIST_URL =  "http://kdncop.cafe24.com/list.jsp";

	
	public ConsultListThread(Handler handler){
		this.handler = handler; 
	}
	
	//context메뉴 > Source > Override/implement Methods
	@Override
	public void run() {
		// TODO Auto-generated method stub
		super.run();
		
		
//		for(int i=0; i<10; i++){
//			Log.d("mylog","Thread 실행..." + i);
//		}
		
		ArrayList<QuestionVO> questionList = new ArrayList<QuestionVO>();
		
		try {

		
			//	1. 팩토리 생성
			SAXParserFactory factory = SAXParserFactory.newInstance();
			//	2. 파서 생성
			SAXParser saxParser = factory.newSAXParser();
			
			ConsultParser parser = new ConsultParser();
			saxParser.parse(REQUEST_LIST_URL, parser );
	
			
			//	3. 파싱		
			questionList= parser.getQuestionList();
	
					
			//4. 파싱한 내용을 요청한 Activity에 전송 		
			//handler.responseRankList(rankList);
			Message msg = new Message();
			msg.what = 0;
			msg.obj = questionList;
			handler.sendMessage(msg);
			//handler.dispatchMessage(msg);
		
		
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		
	}
}
